package com.godme.concurrent.util;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {




    static class MyThread extends Thread{
        private final CyclicBarrier cb;

        public MyThread(String name, CyclicBarrier cb){
            super(name);
            this.cb = cb;
        }

        @Override
        public void run() {
           try{
               System.err.println(getName() + " : step 1");
               cb.await();
               System.err.println(getName() + " : step 2");
               cb.await();
               System.err.println(getName() + " : step 3");
               cb.await();
               System.err.println(getName() + " : step 4");
               cb.await();
               System.err.println(getName() + " : step 5");
               cb.await();
               System.err.println(getName() + " : step 6");
               cb.await();
               System.err.println(getName() + " : step 7");
           }catch (Exception e){

           }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cb = new CyclicBarrier(5);
        for (int i = 0; i < 5; i++) {
            new MyThread("thread-"+i, cb).start();
        }

        Thread.sleep(100000);
    }
}
